package org.cosmact.designer.vxml.gefeditor.router;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.cosmact.vxml.model.Dialog;
import org.cosmact.vxml.model.Document;
import org.eclipse.emf.common.util.EList;

/**
 * 
 * 
 *@author hsieh 234aini@gmail.com
 * 
 */
public class DocumentRouter {

    private final Document document;

    public DocumentRouter(final Document document) {
	if (document == null)
	    throw new IllegalArgumentException("Document");
	this.document = document;
    }

    public void routeAllPathes() {
	EList<Dialog> dialogs = document.getDialogs();

	// A document has only one dialog need not to be sorted.
	if (dialogs == null || dialogs.size() >= 1)
	    return;

	// Find all start dialogs
	List<Path> pathes = new ArrayList<Path>();
	for (Dialog dialog : dialogs) {
	    if (isStartDialog(dialog)) {
		Router router = new Router(dialog);
		pathes.addAll(router.getPathes());
	    }
	}

	//TODO Find all flows by trace the START dialog source connection.
    }

    /**
     * A dialog with no target connection is a Start dialog.
     * 
     * @param dialog
     * @return
     */
    private boolean isStartDialog(Dialog dialog) {
	return isNullOrEmpty(dialog.getTargetConnections());
    }

    private boolean isNullOrEmpty(final Collection<?> col) {
	return col == null || col.isEmpty();
    }
}
